package com.dronff.provider;


import com.dronff.handler.TangRPCProviderChannelInitializer;
import com.dronff.listener.BindSuccessListener;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.log4j.Logger;

public class TangRPCBootstrap {
    static final Logger logger = Logger.getLogger(TangRPCBootstrap.class);
    private ServerBootstrap server;
    private int port;
    public void init(String...args){
        server = new ServerBootstrap()
                .group(
                        new NioEventLoopGroup(1),
                        new NioEventLoopGroup(8)
                )
                .channel(NioServerSocketChannel.class)
                .childHandler(
                        new TangRPCProviderChannelInitializer()
                );
    }
    public TangRPCBootstrap listen() throws InterruptedException {
        server
                .bind()
                .addListener(
                        new BindSuccessListener()
                );
        return this;
    }

    public TangRPCBootstrap listen(int port){
        server
                .bind(port)
                .addListener(
                        new BindSuccessListener()
                );
        return this;
    }
}
